home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / ingres04.lzh / source / libq / IIconvert.c < prev    next >
Encoding:
C/C++ Source or Header  |  1985-01-23  |  2.9 KB  |  148 lines

  1. # include    <ingres.h>
  2. # include    <symbol.h>
  3. # include    <sccs.h>
  4.  
  5. SCCSID(@(#)IIconvert.c    8.1    12/31/84)
  6.  
  7.  
  8. /*
  9. **
  10. **    IIconvert -- Equel run-tme routine to convert
  11. **        numeric values of one type and length, to a
  12. **        (not necessarily) different type and length.
  13. **
  14. **        The source numeric can be i1, i2, i4, f4, or f8.
  15. **
  16. **        The source number will be converted to the
  17. **        type and length specified in the destination.
  18. **        It also must be one of i1, i2, i4, f4, or f8.
  19. **
  20. **    Returns:
  21. **        IIconvert returns 0 if no overflow occured,
  22. **        otherwise it returns -1
  23. */
  24.  
  25.  
  26. IIconvert(inp, outp, sf, slen, df, dlen)
  27. char        *inp;        /* input area */
  28. char        *outp;        /* output area */
  29. int        sf;        /* format of the source number */
  30. int        slen;        /* length of the source number */
  31. int        df;        /* format of the dest */
  32. int        dlen;        /* length of the dest */
  33. {
  34.     char            number[8];    /* dummy buffer */
  35.     register ANYTYPE    *num;
  36.     register int        sl;
  37.     register int        dl;
  38.  
  39.     dl = dlen;
  40.     sl = slen;
  41.     num = (ANYTYPE *) number;
  42.     IIbmove(inp, num,  sl);    /* copy number into buffer */
  43.  
  44.     if (sf != df)
  45.     {
  46.         /* if the source and destination formats are
  47.          * different then the source must be converted
  48.          * to i4 if the dest is int, otherwise to f8 
  49.          */
  50.  
  51.         if (df == FLOAT)    /* {sf == INT} INT->f8 */
  52.         {
  53.             switch (sl)
  54.             {
  55.  
  56.               case 1:
  57.                 num->f8type = num->i1type;    /* i1 to f8 */
  58.                 break;
  59.  
  60.               case 2:
  61.                 num->f8type = num->i2type;    /* i2 to f8 */
  62.                 break;
  63.  
  64.               case 4:
  65.                 num->f8type = num->i4type;    /* i4 to f8 */
  66.             }
  67.             sl = 8;            /* df == INT && sf == FLOAT 
  68.                          * && sl == 8
  69.                          */
  70.         }
  71.         else
  72.         {
  73.             /* {df == INT && sf == FLOAT} FLOAT->i4 */
  74.  
  75.             /* check if float >  2**31 */
  76.             if (sl == 8)
  77.                 num->f4type = num->f8type;    /* convert f8 to f4 */
  78.  
  79.             if (num->f4type > 2147483647.0 || num->f4type < -2147483648.0)
  80.                 return (-1);
  81.             num->i4type = num->f4type;
  82.             sl = 4;
  83.         }
  84.     }
  85.  
  86.     /* number is now the same type as destination */
  87.     /* convert lengths to match */
  88.  
  89.     if (sl != dl)
  90.     {
  91.         /* lengths don't match. convert. */
  92.         if (df == FLOAT)
  93.         {
  94.             if (dl == 8)
  95.                 num->f8type = num->f4type;    /* f4 to f8 */
  96.             else
  97.                 num->f4type = num->f8type;    /* f8 to f4 with rounding */
  98.         }
  99.         else
  100.         {
  101.             switch (dl)
  102.             {
  103.  
  104.               case 1:
  105.                 if (sl == 2)        /* i2 to i1 */
  106.                 {
  107.                     if (num->i2type > 127 || num->i2type < -128)
  108.                         return (-1);
  109.                     num->i1type = num->i2type;    
  110.                 }
  111.                 else            /* i4 to i1 */
  112.                 {
  113.                     if (num->i4type > 127 || num->i4type < -128)
  114.                         return (-1);
  115.                     num->i1type = num->i4type;
  116.                 }
  117.                 break;
  118.  
  119.               case 2:
  120.                 if (sl == 1)        /* i1 to i2 */
  121.                 {
  122.                     num->i2type = num->i1type;    
  123.                 }
  124.                 else            /* i4 to i2 */
  125.                 {
  126.                     if (num->i4type > 32767 || num->i4type <-32768)
  127.                         return (-1);
  128.                     num->i2type = num->i4type;
  129.                 }
  130.                 break;
  131.  
  132.               case 4:
  133.                 if (sl == 1)        /* i1 to i4 */
  134.                     num->i4type = num->i1type;    
  135.                 else            /* i2 to i4 */
  136.                     num->i4type = num->i2type;
  137.             }
  138.         }
  139.     }
  140.  
  141.     /* conversion is complete 
  142.      * copy the result into outp
  143.      */
  144.  
  145.     IIbmove(num, outp, dl);
  146.     return (0);
  147. }
  148.